package com.bdmc.web.controller.api.http;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class WatchSetting {
    private static final String BASE_URL = "http://yg.hxinwatch.com/sdkapi/api/push/hjk/setWatch.htm";


    public static String setBatchParams(String deviceIds, JSONObject args) {
        int succCount=0,errorCount = 0;
        StringBuilder error = new StringBuilder();
        String[] deviceIdArr = deviceIds.split(",");
        for (String deviceId : deviceIdArr) {
            StringBuilder url = new StringBuilder(BASE_URL + "?deviceid=" + deviceId);
            for (String s : args.keySet()) {
                url.append("&").append(s).append("=").append(args.get(s));
            }
            boolean result = sendGetRequest(url.toString());
            if(result){
                succCount++;
            }else{
                errorCount++;
                error.append(deviceId).append(",");
            }
        }
        String errorStr = error.toString();
        if(errorStr.length()>10){
             errorStr = errorStr.substring(0, errorStr.length() - 1);
        }
        return "成功数量："+succCount+",失败："+errorStr;
    }

    // 设置计步器开关
    public static Boolean setStep(String deviceId, Integer stepswitch) {
        String url = BASE_URL + "?deviceid=" + deviceId + "&stepswitch=" + stepswitch;
        return sendGetRequest(url);
    }

    //设置心率时间间隔
    public static Boolean setHeartInterval (String deviceId, Integer interval) {
        String url = BASE_URL + "?deviceid=" + deviceId + "&heartratetimeinterval=" + interval;
        return sendGetRequest(url);
    }

    //设置血压间隔时间
    public static Boolean setBpInterval (String deviceId, Integer interval) {
        String url = BASE_URL + "?deviceid=" + deviceId + "&bpi=" + interval;
        return sendGetRequest(url);
    }

    //设置体温间隔时间
    public static Boolean setTempInterval (String deviceId, Integer interval) {
        String url = BASE_URL + "?deviceid=" + deviceId + "&tpi=" + interval;
        return sendGetRequest(url);
    }

    //设置血氧间隔时间
    public static Boolean setOXInterval (String deviceId, Integer interval) {
        String url = BASE_URL + "?deviceid=" + deviceId + "&oxi=" + interval;
        return sendGetRequest(url);
    }

    // 设置SOS联系人
    public static Boolean setSOSContact(String deviceId, String sos1, String sos2, String sos3) {
        String url = BASE_URL + "?deviceid=" + deviceId + "&sos1=" + sos1 + "&sos2=" + sos2 + "&sos3=" + sos3;
        return sendGetRequest(url);
    }

    // 清除SOS联系人
    public static Boolean clearSOSContact(String deviceId) {
        String url = BASE_URL + "?deviceid=" + deviceId + "&sos1=+&sos2=+&sos3=+";
        return sendGetRequest(url);
    }

    // 设置定位上传间隔(分钟)
    public static Boolean setLocationTimeInterval(String deviceId, int interval) {
        String url = BASE_URL + "?deviceid=" + deviceId + "&locationtimeinterval=" + interval;
        return sendGetRequest(url);
    }

    // 设置心率上传间隔和安全阀值
    public static Boolean setHeartRateParams(String deviceId, int maxHeartRate, int minHeartRate, int interval) {
        String url = BASE_URL + "?deviceid=" + deviceId + "&maxheartrate=" + maxHeartRate + "&minheartrate=" + minHeartRate + "&heartratetimeinterval=" + interval;
        return sendGetRequest(url);
    }

    // 设置血压上传安全阀值
    public static Boolean setBloodPressureParams(String deviceId, int bpLowMin, int bpLowMax, int bpHighMin, int bpHighMax, int bpi) {
        String url = BASE_URL + "?deviceid=" + deviceId + "&bplowmin=" + bpLowMin + "&bplowmax=" + bpLowMax + "&bphighmin=" + bpHighMin + "&bphighmax=" + bpHighMax + "&bpi=" + bpi;
        return sendGetRequest(url);
    }

    // 设置血氧上传安全阀值
    public static Boolean setBloodOxygenParams(String deviceId, int bloodOxMax, int bloodOxMin) {
        String url = BASE_URL + "?deviceid=" + deviceId + "&bloodoxmax=" + bloodOxMax + "&bloodoxmin=" + bloodOxMin;
        return sendGetRequest(url);
    }

    /**
     * 设置闹钟
     * @param deviceId 设备
     * @param name 闹钟名
     * @param time 时间 11:22
     * @param status 状态 0=关闭,1=开启
     * @param audio base64 编码后的语音
     * @return 成功或失败
     */
    public static Boolean setVoiceAlarm (String deviceId, String name,String time, int status,String audio) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("deviceId",deviceId);
        jsonObject.set("name",name);
        jsonObject.set("time",time);
        jsonObject.set("status",status);
        jsonObject.set("audio",audio);
        return sendPostRequest(BASE_URL,jsonObject.toString());
    }

    // 发送HTTP GET请求并返回结果
    private static Boolean sendGetRequest(String url)  {
        String result = HttpUtil.get(url);
        log.info("请求成功！返回结果：" + result);
        JSONObject jsonObject = new JSONObject(result);
        String msg = jsonObject.getStr("msg");
        int code = jsonObject.getInt("code");
        // 判断请求是否成功
        return code == 200;
    }

    private static Boolean sendPostRequest(String url,String body){
        String result = HttpUtil.post(url, body);
        JSONObject jsonObject = new JSONObject(result);
        String msg = jsonObject.getStr("msg");
        int code = jsonObject.getInt("code");
        // 判断请求是否成功
        return code == 200;
    }
}
